package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    /**
     * 用户列表分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(AuthDto dto) {
        //1 检查参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 分页查询
        IPage page = new Page(dto.getPage(),dto.getSize());

        //3 按照不同状态查询 （如果有）
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(dto.getStatus() != null){
            lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }


        //4 按照创建时间倒序
        lambdaQueryWrapper.orderByDesc(ApUserRealname::getCreatedTime);
        page = page(page,lambdaQueryWrapper);

        //5 封装结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 审核用户失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        if(dto==null || dto.getId()==null){
            return ResponseResult.errorResult(400,"参数错误");
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        BeanUtils.copyProperties(dto,apUserRealname);
        apUserRealname.setStatus((short) 2);
        if(StringUtils.isBlank(dto.getMsg())){
            apUserRealname.setReason("审核失败");
        }else{
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealname.setUpdatedTime(new Date());
        updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 审核用户成功
     *
     * @param dto
     * @return
     */
    public ResponseResult authPass(AuthDto dto) {
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(400, "参数错误");
        }
        ApUserRealname apUserRealname = new ApUserRealname();
        BeanUtils.copyProperties(dto, apUserRealname);
        apUserRealname.setStatus((short) 9);
        apUserRealname.setUpdatedTime(new Date());
        updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
